package LinkedList;

/**
 * Description:
 * <p>
 *
 * </p>
 * Created by Kiba 2018/9/19
 */
public class LinkedListTest {

    public static class MyNode {
        int number;
        MyNode next;
    }

    public static MyNode reverse(MyNode head) {

        MyNode reversedHead = null;
        MyNode node = head;
        MyNode prev = null;

        while (node != null) {
            MyNode next = node.next;
            if (next == null) {
                reversedHead = node;
            }

            node.next = prev;
            prev = node;
            node = next;
        }

        return reversedHead;
    }

    public static void traverse(MyNode node) {
        for (; node != null ; node = node.next) {
            System.out.print(node.number + ", ");
        }
    }

    public static void main(String[] args) {
        MyNode[] nodes = new MyNode[10];

        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new MyNode();
            nodes[i].number = i;
        }

        for (int i = 0; i < nodes.length - 1; i++) {
            nodes[i].next = nodes[i + 1];
        }

        MyNode reversedHead = reverse(nodes[0]);

        traverse(reversedHead);
    }

}
